/*
 * IHMneuf.java
 *
 * Created on 25 janvier 2009, 23:40
 */
package IHM_Application;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import miagemosaik.Photo;
import miagemosaik.tools.FileUtils;
import miagemosaik.tools.ImageUtils;

/**
 *
 * @author  Emeline Thiebault
 */
public class IHM extends javax.swing.JFrame {

    private ActionIHM ActionIHMmenu;
    private ActionIHM ActionIHMicone;
    private String path;
    private boolean bddIniated = false;

    /** Creates new form IHMneuf */
    public IHM() {

        //On associe l'action IHM en fonction des deux types de menus
        ActionIHMmenu = new ActionIHM(this, false);
        ActionIHMicone = new ActionIHM(this, true);

        initComponents();

        //Récupere image de fond et la retaille
        ImageIcon logo = new ImageIcon(getClass().getResource("/Icones/logo.jpg"));
        Image logoReSize = logo.getImage().getScaledInstance(860, 640, Image.SCALE_DEFAULT);

        ImageIcon photoFond = new ImageIcon(logoReSize);

        labelPanelPhoto.setIcon(photoFond);

        //On associe l'actionListener
        associerActionListener();

        setResizable(true);
        setLocationRelativeTo(getParent());
        setVisible(true);
    }

    //Méthode d'association des composants aux l'actionListener
    private void associerActionListener() {
        boutonQuitter.addActionListener(ActionIHMmenu);
        boutonIconeQuitter.addActionListener(ActionIHMicone);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelIcone = new javax.swing.JPanel();
        barIcone = new javax.swing.JToolBar();
        boutonIconeQuitter = new javax.swing.JButton();
        boutonIconeEnregistrer = new javax.swing.JButton();
        boutonIconeSupprimer = new javax.swing.JButton();
        boutonIconeInitBase = new javax.swing.JButton();
        boutonIconeOuvrirPhoto = new javax.swing.JButton();
        boutonIconeCreerMosaique = new javax.swing.JButton();
        panelPhoto = new javax.swing.JPanel();
        SeparateurMenu = new javax.swing.JSeparator();
        labelPanelPhoto = new javax.swing.JLabel();
        bar = new javax.swing.JProgressBar();
        barLabel = new javax.swing.JLabel();
        barMenuApplication = new javax.swing.JMenuBar();
        menuApplication = new javax.swing.JMenu();
        boutonOuvrir = new javax.swing.JMenuItem();
        boutonEnregistrer = new javax.swing.JMenuItem();
        boutonQuitter = new javax.swing.JMenuItem();
        menuEdition = new javax.swing.JMenu();
        boutonSupprimer = new javax.swing.JMenuItem();
        menuAction = new javax.swing.JMenu();
        boutonInitBase = new javax.swing.JMenuItem();
        boutonCreerMosaique = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Application Miage Mosaik");
        setResizable(false);

        panelIcone.setMinimumSize(new java.awt.Dimension(1024, 750));

        barIcone.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        barIcone.setForeground(new java.awt.Color(0, 153, 153));
        barIcone.setRollover(true);

        boutonIconeQuitter.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Quitter.gif"))); // NOI18N
        boutonIconeQuitter.setToolTipText("Quitter Miage Mosaik");
        boutonIconeQuitter.setBorderPainted(false);
        boutonIconeQuitter.setFocusable(false);
        boutonIconeQuitter.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        boutonIconeQuitter.setMaximumSize(new java.awt.Dimension(75, 65));
        boutonIconeQuitter.setName("boutonIconeQuitter"); // NOI18N
        boutonIconeQuitter.setPreferredSize(new java.awt.Dimension(55, 65));
        boutonIconeQuitter.setSelected(true);
        boutonIconeQuitter.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        barIcone.add(boutonIconeQuitter);

        boutonIconeEnregistrer.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Enregistrer.gif"))); // NOI18N
        boutonIconeEnregistrer.setToolTipText("Enregistrer la photo");
        boutonIconeEnregistrer.setBorderPainted(false);
        boutonIconeEnregistrer.setFocusable(false);
        boutonIconeEnregistrer.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        boutonIconeEnregistrer.setMaximumSize(new java.awt.Dimension(75, 65));
        boutonIconeEnregistrer.setName("boutonIconeEnregistrer"); // NOI18N
        boutonIconeEnregistrer.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        boutonIconeEnregistrer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boutonIconeEnregistrerActionPerformed(evt);
            }
        });
        barIcone.add(boutonIconeEnregistrer);

        boutonIconeSupprimer.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Supprimer.gif"))); // NOI18N
        boutonIconeSupprimer.setToolTipText("Supprimer photo");
        boutonIconeSupprimer.setBorderPainted(false);
        boutonIconeSupprimer.setFocusable(false);
        boutonIconeSupprimer.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        boutonIconeSupprimer.setMaximumSize(new java.awt.Dimension(65, 65));
        boutonIconeSupprimer.setName("boutonIconeSupprimer"); // NOI18N
        boutonIconeSupprimer.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        boutonIconeSupprimer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boutonIconeSupprimerActionPerformed(evt);
            }
        });
        barIcone.add(boutonIconeSupprimer);

        boutonIconeInitBase.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/InitBase.gif"))); // NOI18N
        boutonIconeInitBase.setToolTipText("Initialiser la base d'images");
        boutonIconeInitBase.setBorderPainted(false);
        boutonIconeInitBase.setFocusable(false);
        boutonIconeInitBase.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        boutonIconeInitBase.setMaximumSize(new java.awt.Dimension(75, 65));
        boutonIconeInitBase.setName("boutonIconeInitBase"); // NOI18N
        boutonIconeInitBase.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        boutonIconeInitBase.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boutonIconeInitBaseActionPerformed(evt);
            }
        });
        barIcone.add(boutonIconeInitBase);

        boutonIconeOuvrirPhoto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/OuvrirPhoto.gif"))); // NOI18N
        boutonIconeOuvrirPhoto.setToolTipText("Ouvrir photo");
        boutonIconeOuvrirPhoto.setBorderPainted(false);
        boutonIconeOuvrirPhoto.setFocusable(false);
        boutonIconeOuvrirPhoto.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        boutonIconeOuvrirPhoto.setMaximumSize(new java.awt.Dimension(75, 65));
        boutonIconeOuvrirPhoto.setName("boutonIconeOuvrirPhoto"); // NOI18N
        boutonIconeOuvrirPhoto.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        boutonIconeOuvrirPhoto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boutonIconeOuvrirPhotoActionPerformed(evt);
            }
        });
        barIcone.add(boutonIconeOuvrirPhoto);

        boutonIconeCreerMosaique.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/CreerMosaik.gif"))); // NOI18N
        boutonIconeCreerMosaique.setToolTipText("Créer la mosaïque");
        boutonIconeCreerMosaique.setBorderPainted(false);
        boutonIconeCreerMosaique.setFocusable(false);
        boutonIconeCreerMosaique.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        boutonIconeCreerMosaique.setMaximumSize(new java.awt.Dimension(75, 65));
        boutonIconeCreerMosaique.setName("boutonIconeCreerMosaique"); // NOI18N
        boutonIconeCreerMosaique.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        boutonIconeCreerMosaique.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boutonIconeCreerMosaiqueActionPerformed(evt);
            }
        });
        barIcone.add(boutonIconeCreerMosaique);

        panelPhoto.setMinimumSize(new java.awt.Dimension(848, 636));
        panelPhoto.setPreferredSize(new java.awt.Dimension(848, 636));

        labelPanelPhoto.setText(" ");

        javax.swing.GroupLayout panelPhotoLayout = new javax.swing.GroupLayout(panelPhoto);
        panelPhoto.setLayout(panelPhotoLayout);
        panelPhotoLayout.setHorizontalGroup(
            panelPhotoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(SeparateurMenu, javax.swing.GroupLayout.DEFAULT_SIZE, 1278, Short.MAX_VALUE)
            .addGroup(panelPhotoLayout.createSequentialGroup()
                .addGap(211, 211, 211)
                .addComponent(labelPanelPhoto, javax.swing.GroupLayout.PREFERRED_SIZE, 848, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(219, Short.MAX_VALUE))
        );
        panelPhotoLayout.setVerticalGroup(
            panelPhotoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelPhotoLayout.createSequentialGroup()
                .addComponent(SeparateurMenu, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelPanelPhoto, javax.swing.GroupLayout.PREFERRED_SIZE, 636, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout panelIconeLayout = new javax.swing.GroupLayout(panelIcone);
        panelIcone.setLayout(panelIconeLayout);
        panelIconeLayout.setHorizontalGroup(
            panelIconeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelIconeLayout.createSequentialGroup()
                .addComponent(barIcone, javax.swing.GroupLayout.PREFERRED_SIZE, 440, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 404, Short.MAX_VALUE)
                .addGroup(panelIconeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(barLabel))
                .addGap(288, 288, 288))
            .addComponent(panelPhoto, javax.swing.GroupLayout.DEFAULT_SIZE, 1278, Short.MAX_VALUE)
        );
        panelIconeLayout.setVerticalGroup(
            panelIconeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelIconeLayout.createSequentialGroup()
                .addGroup(panelIconeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(barIcone, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(panelIconeLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(barLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(bar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelPhoto, javax.swing.GroupLayout.PREFERRED_SIZE, 655, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        barMenuApplication.setName("barMenuApplication"); // NOI18N

        menuApplication.setText("Application");
        menuApplication.setName("menuApplication"); // NOI18N

        boutonOuvrir.setText("Ouvrir...");
        boutonOuvrir.setName("boutonOuvrir"); // NOI18N
        boutonOuvrir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boutonOuvrirActionPerformed(evt);
            }
        });
        menuApplication.add(boutonOuvrir);

        boutonEnregistrer.setText("Enregistrer sous...");
        boutonEnregistrer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boutonEnregistrerActionPerformed(evt);
            }
        });
        menuApplication.add(boutonEnregistrer);

        boutonQuitter.setText("Quitter");
        boutonQuitter.setName("boutonQuitter"); // NOI18N
        menuApplication.add(boutonQuitter);

        barMenuApplication.add(menuApplication);

        menuEdition.setText("Edition");

        boutonSupprimer.setText("Supprimer photo");
        boutonSupprimer.setName("boutonSupprimer"); // NOI18N
        boutonSupprimer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boutonSupprimerActionPerformed(evt);
            }
        });
        menuEdition.add(boutonSupprimer);

        barMenuApplication.add(menuEdition);

        menuAction.setText("Action");

        boutonInitBase.setText("Initialiser Base");
        boutonInitBase.setName("boutonInitBase"); // NOI18N
        boutonInitBase.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boutonInitBaseActionPerformed(evt);
            }
        });
        menuAction.add(boutonInitBase);

        boutonCreerMosaique.setText("Créer mosaïque");
        boutonCreerMosaique.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boutonCreerMosaiqueActionPerformed(evt);
            }
        });
        menuAction.add(boutonCreerMosaique);

        barMenuApplication.add(menuAction);

        setJMenuBar(barMenuApplication);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelIcone, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(panelIcone, javax.swing.GroupLayout.PREFERRED_SIZE, 704, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void boutonIconeCreerMosaiqueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boutonIconeCreerMosaiqueActionPerformed

    panelIcone.validate();
    if (!bddIniated) {
        JOptionPane.showMessageDialog(null, "Aucune base d'images mémorisée", "Opération impossible", JOptionPane.INFORMATION_MESSAGE);
    } else {
        barLabel.setText("Création de la mosaique");
        bar.setValue(0);
        bar.setStringPainted(true);
        ImageIcon img = (ImageIcon) labelPanelPhoto.getIcon();
        /** On s'assure que l'image est complètement chargée */
        Image image = img.getImage();

        /** On crée la nouvelle image */
        BufferedImage bufferedImage = new BufferedImage(
                image.getWidth(null),
                image.getHeight(null),
                BufferedImage.TYPE_INT_RGB);

        Graphics g = bufferedImage.createGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();

        //on s'assure que ce n'est pas null
        if (bufferedImage != null) {
            Color[][] colorTab = ImageUtils.getSplittedImage(bufferedImage);
            Photo[][] mosaique = ImageUtils.getMosaiqueTab(colorTab);
            Image imageFinale = ImageUtils.creeImageFinale(mosaique, path);
            labelPanelPhoto.setIcon(new ImageIcon(imageFinale));
            bar.setValue(100);
        }
    }
}//GEN-LAST:event_boutonIconeCreerMosaiqueActionPerformed

private void boutonInitBaseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boutonInitBaseActionPerformed
    barLabel.setText("initialisation de la BDD");
    bar.setValue(0);
    bar.setStringPainted(true);
    JFileChooser choixCharger = new JFileChooser();
    choixCharger.setFileSelectionMode((JFileChooser.DIRECTORIES_ONLY));
    choixCharger.setCurrentDirectory(new File("."));
    choixCharger.setApproveButtonText("Ouvrir");
    if (choixCharger.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        path = choixCharger.getSelectedFile().getAbsolutePath();
        try {
            int nb = FileUtils.initIndex(path);
            bddIniated = true;
            bar.setValue(100);
            JOptionPane.showMessageDialog(this, nb + " images ont été initialisées");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}//GEN-LAST:event_boutonInitBaseActionPerformed

private void boutonOuvrirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boutonOuvrirActionPerformed
    JFileChooser choixCharger = new JFileChooser();
    choixCharger.setCurrentDirectory(new File("."));
    FileMasque jpg = new FileMasque(new String("jpg"), "Fichiers jpg");
    choixCharger.addChoosableFileFilter(jpg);

    choixCharger.setApproveButtonText("Ouvrir");
    if (choixCharger.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        String path = choixCharger.getSelectedFile().getAbsolutePath();
        labelPanelPhoto.setIcon(ImageUtils.openPhoto(path));
    }
}//GEN-LAST:event_boutonOuvrirActionPerformed

private void boutonIconeOuvrirPhotoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boutonIconeOuvrirPhotoActionPerformed
    JFileChooser choixCharger = new JFileChooser();
    choixCharger.setCurrentDirectory(new File("."));
    FileMasque jpg = new FileMasque(new String("jpg"), "Fichiers jpg");
    choixCharger.addChoosableFileFilter(jpg);

    choixCharger.setApproveButtonText("Ouvrir");
    if (choixCharger.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        String path = choixCharger.getSelectedFile().getAbsolutePath();
        labelPanelPhoto.setIcon(ImageUtils.openPhoto(path));
    }
}//GEN-LAST:event_boutonIconeOuvrirPhotoActionPerformed

private void boutonIconeInitBaseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boutonIconeInitBaseActionPerformed
    barLabel.setText("initialisation de la BDD");
    bar.setValue(0);
    bar.setStringPainted(true);
    JFileChooser choixCharger = new JFileChooser();
    choixCharger.setFileSelectionMode((JFileChooser.DIRECTORIES_ONLY));
    choixCharger.setCurrentDirectory(new File("."));
    choixCharger.setApproveButtonText("Ouvrir");
    if (choixCharger.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        path = choixCharger.getSelectedFile().getAbsolutePath();
        try {
            int nb = FileUtils.initIndex(path);
            bddIniated = true;
            bar.setValue(100);
            JOptionPane.showMessageDialog(this, nb + " images ont été initialisées");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}//GEN-LAST:event_boutonIconeInitBaseActionPerformed

private void boutonSupprimerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boutonSupprimerActionPerformed
    int confirmation = JOptionPane.showConfirmDialog(null,
            "Souhaitez-vous réellement supprimer la photo", "Suppression de l'image en cours",
            JOptionPane.YES_NO_OPTION);
    //Demande de confirmation de la est Supprime
    if (confirmation == JOptionPane.YES_OPTION) {
        ImageIcon logo = new ImageIcon(getClass().getResource("/Icones/logo.jpg"));
        Image logoReSize = logo.getImage().getScaledInstance(860, 640, Image.SCALE_DEFAULT);

        ImageIcon photoFond = new ImageIcon(logoReSize);

        labelPanelPhoto.setIcon(photoFond);
    }
}//GEN-LAST:event_boutonSupprimerActionPerformed

private void boutonIconeSupprimerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boutonIconeSupprimerActionPerformed
    int confirmation = JOptionPane.showConfirmDialog(null,
            "Souhaitez-vous réellement supprimer la photo", "Suppression de l'image en cours",
            JOptionPane.YES_NO_OPTION);
    //Demande de confirmation de la est Supprime
    if (confirmation == JOptionPane.YES_OPTION) {
        ImageIcon logo = new ImageIcon(getClass().getResource("/Icones/logo.jpg"));
        Image logoReSize = logo.getImage().getScaledInstance(860, 640, Image.SCALE_DEFAULT);

        ImageIcon photoFond = new ImageIcon(logoReSize);

        labelPanelPhoto.setIcon(photoFond);
    }
}//GEN-LAST:event_boutonIconeSupprimerActionPerformed

private void boutonEnregistrerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boutonEnregistrerActionPerformed

    JFileChooser choixEnregistrer = new JFileChooser();
    choixEnregistrer.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    choixEnregistrer.setCurrentDirectory(new File("."));
    choixEnregistrer.setAcceptAllFileFilterUsed(false);
    FileFilter jpeg = new FileFilter() {

        public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            } else if (f.getName().endsWith(".jpg")) {
                return true;
            } else {
                return false;
            }
        }

        public String getDescription() {
            return "Images JPEG";
        }
    };
    choixEnregistrer.addChoosableFileFilter(jpeg);
    choixEnregistrer.setApproveButtonText("Enregistrer");
    File f = new File("./miagemosaik.jpg");
    choixEnregistrer.setSelectedFile(f);

    if (choixEnregistrer.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {

        String path = choixEnregistrer.getSelectedFile().getAbsolutePath();

        try {
            ImageUtils.saveImage(path, (ImageIcon) labelPanelPhoto.getIcon());
        } catch (IOException ex) {
            Logger.getLogger(IHM.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}//GEN-LAST:event_boutonEnregistrerActionPerformed

private void boutonIconeEnregistrerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boutonIconeEnregistrerActionPerformed
    JFileChooser choixEnregistrer = new JFileChooser();
    choixEnregistrer.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    choixEnregistrer.setCurrentDirectory(new File("."));
    choixEnregistrer.setAcceptAllFileFilterUsed(false);
    FileFilter jpeg = new FileFilter() {

        public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            } else if (f.getName().endsWith(".jpg")) {
                return true;
            } else {
                return false;
            }
        }

        public String getDescription() {
            return "Images JPEG";
        }
    };
    choixEnregistrer.addChoosableFileFilter(jpeg);
    choixEnregistrer.setApproveButtonText("Enregistrer");
    File f = new File("./miagemosaik.jpg");
    choixEnregistrer.setSelectedFile(f);

    if (choixEnregistrer.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {

        String path = choixEnregistrer.getSelectedFile().getAbsolutePath();

        try {
            ImageUtils.saveImage(path, (ImageIcon) labelPanelPhoto.getIcon());
        } catch (IOException ex) {
            Logger.getLogger(IHM.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}//GEN-LAST:event_boutonIconeEnregistrerActionPerformed

private void boutonCreerMosaiqueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boutonCreerMosaiqueActionPerformed
    if (!bddIniated) {
        JOptionPane.showMessageDialog(null, "Aucune base d'images mémorisée", "Opération impossible", JOptionPane.INFORMATION_MESSAGE);
    } else {
        barLabel.setText("Création de la mosaique");
        bar.setValue(0);
        bar.setStringPainted(true);
        ImageIcon img = (ImageIcon) labelPanelPhoto.getIcon();
        /** On s'assure que l'image est complètement chargée */
        Image image = img.getImage();

        /** On crée la nouvelle image */
        BufferedImage bufferedImage = new BufferedImage(
                image.getWidth(null),
                image.getHeight(null),
                BufferedImage.TYPE_INT_RGB);

        Graphics g = bufferedImage.createGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();

        //on s'assure que ce n'est pas null
        if (bufferedImage != null) {
            Color[][] colorTab = ImageUtils.getSplittedImage(bufferedImage);
            Photo[][] mosaique = ImageUtils.getMosaiqueTab(colorTab);
            Image imageFinale = ImageUtils.creeImageFinale(mosaique, path);
            labelPanelPhoto.setIcon(new ImageIcon(imageFinale));
            bar.setValue(100);
        }
    }
}//GEN-LAST:event_boutonCreerMosaiqueActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSeparator SeparateurMenu;
    private javax.swing.JProgressBar bar;
    private javax.swing.JToolBar barIcone;
    private javax.swing.JLabel barLabel;
    private javax.swing.JMenuBar barMenuApplication;
    private javax.swing.JMenuItem boutonCreerMosaique;
    private javax.swing.JMenuItem boutonEnregistrer;
    private javax.swing.JButton boutonIconeCreerMosaique;
    private javax.swing.JButton boutonIconeEnregistrer;
    private javax.swing.JButton boutonIconeInitBase;
    private javax.swing.JButton boutonIconeOuvrirPhoto;
    private javax.swing.JButton boutonIconeQuitter;
    private javax.swing.JButton boutonIconeSupprimer;
    private javax.swing.JMenuItem boutonInitBase;
    private javax.swing.JMenuItem boutonOuvrir;
    private javax.swing.JMenuItem boutonQuitter;
    private javax.swing.JMenuItem boutonSupprimer;
    private javax.swing.JLabel labelPanelPhoto;
    private javax.swing.JMenu menuAction;
    private javax.swing.JMenu menuApplication;
    private javax.swing.JMenu menuEdition;
    private javax.swing.JPanel panelIcone;
    private javax.swing.JPanel panelPhoto;
    // End of variables declaration//GEN-END:variables
}
